package cn.itcast.nio.c4;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

import static cn.itcast.nio.c1.ByteBufferUtil.debugAll;

@Slf4j
public class Server {

    public static void main(String[] args) throws IOException {
        //0.ByteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(16);

        //1.创建服务器
        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);

        //2.绑定监听端口
        ssc.bind(new InetSocketAddress(8080));

        //3.accept
        List<SocketChannel> channels = new ArrayList<>();
        while (true) {
            //4.accept 简历与客户端链接，SocketChannel用来与客户端之间通信
//            log.debug("connecting...");
            SocketChannel sc = ssc.accept();//阻塞方法，线程停止运行
            if (sc != null) {
                log.debug("connected... {}", sc);
                sc.configureBlocking(false);
                channels.add(sc);
            }

            for (SocketChannel channel : channels) {
                //5.接收客户端发送的数据
//                log.debug("before read... {}", channel);
                int read = channel.read(buffer);//阻塞方法，线程停止运行
                if (read>0){
                    buffer.flip();

                    debugAll(buffer);
                    buffer.clear();
                    log.debug("after read... {}", channel);
                }
            }
        }
    }
}
